using System;
using System.Collections.Generic;

namespace System.IO.DropBox
{
    public class DBXResultWithCompletionFunction<T> : IDisposable where T : Stream
    {
        public DBXResultWithCompletionFunction(T stream, Action<DBXResultWithCompletionFunction<T>> closeCallback)
        {
            this.closeCallback = closeCallback;
            this.stream = stream;
            this.StateVars = new Dictionary<string, object>();
        }

        private readonly Action<DBXResultWithCompletionFunction<T>> closeCallback;
        public T stream;

        public Dictionary<string, object> StateVars
        {
            get;
            set;
        }

        public void Close()
        {
            try
            {
                this.closeCallback(this);
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                    stream.Dispose();
                }
            }
        }

        #region IDisposable Members

        public void Dispose()
        {
            Close();
        }

        #endregion IDisposable Members
    }
}